﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum EnemyState
{
    Enter,
    Idle,
    Move,
    Attack,
    Change,
    MoveAttack,
}

/// <summary>
/// EnemyAI
/// </summary>
public class EnemyMove : MonoBehaviour {

    public float speed = 2;
    public EnemyState state;

    public int behaviourID = 0;
    public EnemyState[] behaviors;//行为数组

    EnemyGunManager gunManager;
    float lastTime = 0;
    Vector3 direction;

    private void Awake()
    {
        gunManager = this.GetComponent<EnemyGunManager>();
    }

    void NextBehaviour()
    {
        if(behaviourID >= behaviors.Length)
        {
            behaviourID = 0;
        }
        lastTime = Time.time;
        state = behaviors[behaviourID++];
    }
    //有限状态机
    private void Update()
    {
        switch (state)
        {
            case EnemyState.Enter:
                OnEnter();
                break;
            case EnemyState.Idle:
                OnIdle();
                break;
            case EnemyState.Move:
                OnMove();
                break;
            case EnemyState.Attack:
                OnAttack();
                break;
            case EnemyState.Change:
                OnChange();
                break;
            case EnemyState.MoveAttack:
                OnMoveAttack();
                break;
            default:
                break;
        }
    }

    void OnEnter()
    {
        transform.Translate(Vector3.down * speed * Time.deltaTime);
        if(transform.position.y<=3)
        {
            //计算移动方向
            if (transform.position.x < 0) 
                direction = Vector3.right;
            else
                direction = Vector3.left;
            
            NextBehaviour();
        }
    }
    void OnIdle()
    {
        if(Time.time- lastTime > 2)
        {
            NextBehaviour();
        }
    }
    void OnMove()
    {
        transform.Translate(direction * speed * Time.deltaTime);
        if (transform.position.x >= 1.3f || transform.position.x <= -1.3f)
        {
            if (transform.position.x < 0)
                direction = Vector3.right;
            else
                direction = Vector3.left;

            NextBehaviour();
        }
    }
    void OnAttack()
    {
        gunManager.Fire();
        if (Time.time-lastTime>gunManager.FireDuration())
        {
            NextBehaviour();
        }
    }

    void OnChange()
    {
        gunManager.ChangeGun();
        NextBehaviour();
    }

    int flag = 1;
    //左右移动，释放子弹
    void OnMoveAttack()
    {
        transform.Translate(Vector2.right * flag * speed * Time.deltaTime);
        if (transform.position.x >= 1f)
        {
            flag = -1;
        }
        if(transform.position.x <= -1f)
        {
            flag = 1;
        }

        gunManager.Fire();
        if (Time.time - lastTime > gunManager.FireDuration())
        {
            NextBehaviour();
        }
    }
}
